/*
 * Copyright (C) 2021 Baidu, Inc. All Rights Reserved.
 */
package com.chukun.rpc.protocol.handler;

import com.chukun.rpc.core.TinyRpcFuture;
import com.chukun.rpc.core.TinyRpcRequestHolder;
import com.chukun.rpc.core.TinyRpcResponse;
import com.chukun.rpc.protocol.protocol.TinyRpcProtocol;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * <p>rpc框架响应处理器<p/>
 *
 * @author chukun
 * @version 1.0
 * @since 1.0-SNAPSHOT
 */
public class TinyRpcResponseHandler extends SimpleChannelInboundHandler<TinyRpcProtocol<TinyRpcResponse>> {
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext,
                                TinyRpcProtocol<TinyRpcResponse> response) throws Exception {
        // 获取requestId
        long requestId = response.getHeader().getRequestId();
        // 将request从request的future列表中移除
        TinyRpcFuture<TinyRpcResponse> future =
                TinyRpcRequestHolder.getRequestMap().remove(requestId);
        future.getPromise().setSuccess(response.getBody());
    }
}
